#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init() {
    /////////////////初始化按钮//////////////、
    ui->btnCameraOpen->setText(tr("启动"));
    setCameraWidgetEnable(false);
    ///////////////////初始化相机//////////////
    camera = new Camera();
    connect(&camera->timer, &QTimer::timeout, this, &MainWindow::updateImg);
    //曝光slider和spinBox初始化绑定
    void (QSpinBox:: *spinBoxValueChanged)(int) = &QSpinBox::valueChanged;
    connect(ui->exposureSlider, &QSlider::valueChanged, ui->exposureSpinBox, &QSpinBox::setValue);
    connect(ui->exposureSpinBox, spinBoxValueChanged, ui->exposureSlider, &QSlider::setValue);
    connect(ui->exposureSlider, &QSlider::valueChanged, this, [=]() {
       camera->setExposureTime(ui->exposureSlider->value());
    }); //曝光调节函数出发
    //帧率slider和spinBox初始化绑定
    connect(ui->fpsSlider, &QSlider::valueChanged, ui->fpsSpinBox, &QSpinBox::setValue);
    connect(ui->fpsSpinBox, spinBoxValueChanged, ui->fpsSlider, &QSlider::setValue);
    connect(ui->fpsSlider, &QSlider::valueChanged, this, [=]() {
        camera->setFPS(ui->fpsSlider->value());
    }); //fps调节函数触发
    ////////////////////初始化人脸和眼睛识别//////////////
    FaceDetect::setFaceCascadeFile(
                "../../resource/data/faces/haarcascades/haarcascade_frontalface_alt2.xml");
    FaceDetect::setEyeCascadeFile(
                "../../resource/data/faces/haarcascades/haarcascade_eye_tree_eyeglasses.xml");
    FaceDetect::setDetectModel(
                "../../resource/data/faces/model/MyFacePCAModel.xml");
    ////////////////////初始化显示视频用的Label//////////////////
    QImage initImg(ui->imgLabel->size(), QImage::Format_RGB888);
    initImg.fill(0);
    ui->imgLabel->setPixmap(QPixmap::fromImage(initImg));
    ////////////////////初始化计时器////////////////////////
    connect(&runtimeTimer, &QTimer::timeout, this->ui->runtimeLabel, [&]() {
        if (++runtime[0] == 60) {
            runtime[0] = 0;
            if (++runtime[1] == 60) {
                runtime[1] = 0;
                if (++runtime[2] == 24) {
                    runtime[2] = 0;
                    runtime[3]++;
                }
            }
        }
        QString nowTime = QString("%1天%2时%3分%4秒").arg(runtime[3], 2).arg(runtime[2], 2).arg(runtime[1], 2).arg(runtime[0], 2);
        ui->runtimeLabel->setText(nowTime);
    });
    runtimeTimer.start(1000);
}

void MainWindow::paintEvent(QPaintEvent *e) {
    //窗口重绘制时调用
}

void MainWindow::drawImgOnLabel(const cv::Mat& inputImg, QLabel* imgLabel) {
    //使用camera的srcImg绘制图像
    QImage img = QImage((uchar*)(inputImg.data),
                        inputImg.cols, inputImg.rows,
                        QImage::Format_RGB888);
    imgLabel->setPixmap(QPixmap::fromImage(img));
    imgLabel->resize(img.size());
    imgLabel->show();
}

void MainWindow::updateImg() {
//    camera->videoCap >> camera->srcImg;
    camera->grab();
    camera->retrieve(camera->srcImg);
    if ( ui->btnFaceDetect->isChecked() && ! ui->btnEyeDetect->isChecked() ) {
        FaceDetect::detectFaceAndDraw(camera->srcImg, 1.1, false);
    }
    else if (ui->btnFaceDetect->isChecked() && ui->btnEyeDetect->isChecked()) {
        FaceDetect::detectFaceAndDraw(camera->srcImg, 1.1, true);
    }
    if (camera->srcImg.data) {
        cv::cvtColor(camera->srcImg, camera->srcImg, CV_BGR2RGB);
        drawImgOnLabel(camera->srcImg, ui->imgLabel);

//        ui->cameraGroupBox->update();

    }
}

//设置相机按钮不可用
void MainWindow::setCameraWidgetEnable(bool state) {
    if (state) {
        ui->btnCameraOpen->setStyleSheet("background-color:green");
        ui->btnFaceDetect->setStyleSheet("background-color:green");
        ui->btnEyeDetect->setStyleSheet("background-color:green");
        ui->btnSupervise->setStyleSheet("background-color:green");
    }
    else {
        ui->btnCameraOpen->setStyleSheet("background-color:gray");
        ui->btnFaceDetect->setStyleSheet("background-color:gray");
        ui->btnEyeDetect->setStyleSheet("background-color:gray");
        ui->btnSupervise->setStyleSheet("background-color:gray");
    }
    ui->btnCameraOpen->setEnabled(state);
    ui->btnFaceDetect->setEnabled(state);
    ui->btnEyeDetect->setEnabled(state);
    ui->btnSupervise->setEnabled(state);
    //曝光组件失能
    ui->exposureSlider->setEnabled(state);
    ui->exposureSpinBox->setEnabled(state);
    //帧率组件失能
    ui->fpsSlider->setEnabled(state);
    ui->fpsSpinBox->setEnabled(state);
}

void MainWindow::on_btnCameraOpen_clicked(bool checked)
{
    if (checked) {  //开启相机
        ui->btnCameraOpen->setText(tr("暂停"));
        ui->btnCameraOpen->setStyleSheet("background-color:red");
        camera->start();
    }
    else  {     //暂停相机
        ui->btnCameraOpen->setText(tr("开启"));
        ui->btnCameraOpen->setStyleSheet("background-color:green");
        camera->pause();
        camera->showPauseImg();
        drawImgOnLabel(camera->srcImg, ui->imgLabel);
    }
}

void MainWindow::on_btnCameraConnect_clicked(bool checked)
{
    //按下了按钮
    if (checked) {  //连接相机
        ui->btnCameraConnect->setText(tr("断开相机"));
        ui->btnCameraConnect->setStyleSheet("background-color:red");
        setCameraWidgetEnable(true);
        camera->open(0);
        //后期可以从yaml配置文件中设置变量
        int fps = 60, exposureTime = 100;
        camera->setParameters(cv::Size(640, 480), exposureTime, fps);
        ui->exposureSlider->setValue(exposureTime);
        ui->fpsSlider->setValue(fps);
    }
    else  { //断开相机
        ui->btnCameraConnect->setText(tr("连接相机"));
        ui->btnCameraConnect->setStyleSheet("background-color:green");
        //设置相机组件相关按钮不可用
        setCameraWidgetEnable(false);
        camera->pause();
        camera->close(0);
    }
}

void MainWindow::on_btnFaceDetect_clicked(bool checked)
{
    if (checked) {  //关闭人脸识别
        ui->btnFaceDetect->setText(tr("关闭人脸识别"));
        ui->btnFaceDetect->setStyleSheet("background-color:red");
    }
    else {  //开启人脸识别
        ui->btnFaceDetect->setText(tr("开启人脸识别"));
        ui->btnFaceDetect->setStyleSheet("background-color:green");
    }
}

void MainWindow::on_btnEyeDetect_clicked(bool checked)
{
    if (checked) {  //关闭眼睛识别
        ui->btnEyeDetect->setText(tr("关闭眼睛识别"));
        ui->btnEyeDetect->setStyleSheet("background-color:red");
    }
    else {  //开启人脸识别
        ui->btnEyeDetect->setText(tr("开启眼睛识别"));
        ui->btnEyeDetect->setStyleSheet("background-color:green");
    }
}

void MainWindow::on_btnSupervise_clicked(bool checked)
{
    if (checked) {  //关闭眼睛识别
        ui->btnSupervise->setText(tr("关闭监督"));
        ui->btnSupervise->setStyleSheet("background-color:red");
    }
    else {  //开启人脸识别
        ui->btnSupervise->setText(tr("开始监督"));
        ui->btnSupervise->setStyleSheet("background-color:green");
    }
}
